package com.quasar.hpatient.view;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.custommodel.OutJionValueDataBean;
import com.quasar.hpatient.model.custommodel.TemperatureTableItem;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.litePalmodel.DailyItemInfodb;
import com.quasar.hpatient.model.medicalmodel.DailyRecordDetailsData;
import com.quasar.hpatient.onInterface.OndailyRecordListener;
import com.quasar.hpatient.presenter.DailyRecordPresenter;
import com.quasar.hpatient.utils.DateUtil;
import com.quasar.hpatient.utils.OutliersValues;
import com.quasar.hpatient.view.adapter.DailyLinAdapter;
import com.quasar.hpatient.widget.lineChartView;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.view.ColumnChartView;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * 历史日常数据折线图
 */

@EActivity(R.layout.activity_daily_lin)
public class   DailyLinActivity extends BaseActivity implements OndailyRecordListener {

    private DailyRecordPresenter dailyRecordPresenter;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    //判断下一页是否存在数据
    //分页
    int page = 0;

    //检测item名称
    @Extra
    String itemName;
    //检测时间
    @Extra
    String dataTime;

    @ViewById(R.id.main_toolbar)
    Toolbar main_toolbar;
    @ViewById(R.id.rv_list)
    RecyclerView rv_list;


    DailyItemInfodb iteminfo;//查询当前界面的itemid
    List<DailyItemInfodb> dailyItemInfoList;
    DailyLinAdapter dailyLinAdapter;


    @AfterViews
    void init() {
        initView();
        initData();
    }

    @Override
    protected void initView() {
        initToolbar();
        setSupportActionBar(main_toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_action_back);
        rv_list.setLayoutManager(new LinearLayoutManager(this));
        rv_list.setHasFixedSize(true);
        rv_list.setNestedScrollingEnabled(false);
    }


    //初始化adapter布局
    public void initJpshAdapter() {

        ViseLog.d("传过来的值" + itemName);

        dailyLinAdapter = new DailyLinAdapter(null);
        if (itemName != null) {
            if (!itemName.contains(ConstantManger.INOUTPUTVAL)) {
                viewhead = getLayoutInflater().inflate(R.layout.dailylin_head_layout, (ViewGroup) rv_list.getParent(), false);
                dailyLinAdapter.addHeaderView(viewhead);
            } else {
                viewheadout = getLayoutInflater().inflate(R.layout.output_intake_layout, (ViewGroup) rv_list.getParent(), false);
                dailyLinAdapter.addHeaderView(viewheadout);
            }
            rv_list.setAdapter(dailyLinAdapter);
        }
    }

    @Override
    protected void initData() {

        dailyItemInfoList = MainAppliaction.getInstance().getDailyItemInfoList();
        for (int i = 0; i < dailyItemInfoList.size(); i++) {
            if (dailyItemInfoList.get(i).getName().equals(itemName)) {

                ViseLog.d("標題++++" + itemName + "*****" + dailyItemInfoList.get(i).getName());
                iteminfo = dailyItemInfoList.get(i);
                break;
            }
        }

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("分析图表-" + itemName);
        if (iteminfo != null) {
            stringBuffer.append("\t(" + iteminfo.getDimensionid() + ")");
        } else {
            stringBuffer.append("\t毫升(ml)");
        }
        //标题显示
        getSupportActionBar().setTitle(stringBuffer.toString());

        //计算时间
        Date date = null;
        try {
            date = sdf.parse(dataTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ViseLog.d("时间" + date);

        stats = sdf.format(getDateAfter(date, -7));
        initJpshAdapter();
        dailyRecordPresenter = new DailyRecordPresenter(this);
        //查询一天日常记录
        dailyRecordPresenter.GetUserOneDayDailyRecordData(itemName, dataTime, type);
        showDialog();

    }

    final String type = "7";

    String stats;

    public Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        ViseLog.d("时间推算" + sdf.format(now.getTime()));
        return now.getTime();
    }


    //收缩压
    public int shrink(String value) {//ddd

        Float judgevalue = Float.parseFloat(value);

        if (judgevalue < 80) {
            return 1;
        }
        if (judgevalue > 80.1 && judgevalue < 120.1) {
            return 2;
        }
        if (judgevalue > 120.1 && judgevalue < 139.1) {
            return 3;
        }
        if (judgevalue > 139.1 && judgevalue < 159.1) {
            return 4;
        }
        if (judgevalue > 159.1 && judgevalue < 179.1) {
            return 5;
        }
        if (judgevalue > 179.1) {
            return 6;
        }
        return 0;
    }

    //舒张压
    public int Diastole(String value) {//ppp
        Float judgevalue = Float.parseFloat(value);

        if (judgevalue < 40.1) {
            return 1;
        }
        if (judgevalue > 40.1 && judgevalue < 80.1) {
            return 2;
        }
        if (judgevalue > 80.1 && judgevalue < 89.1) {
            return 3;
        }
        if (judgevalue > 89.1 && judgevalue < 99.1) {
            return 4;
        }
        if (judgevalue > 99.1 && judgevalue < 109.1) {
            return 5;
        }
        if (judgevalue > 109.1) {
            return 6;
        }

        return 0;
    }


    //舒张压statslow
    public int Valstats(String statsHigh, String statslow) {
        int stats = 0;

        if (shrink(statsHigh) >= 4 && Diastole(statslow) < 4) {

            return 7;
        }
        if (shrink(statsHigh) > Diastole(statslow)) {
            stats = shrink(statsHigh);
        } else if (shrink(statsHigh) < Diastole(statslow)) {
            stats = Diastole(statslow);
        } else {
            stats = Diastole(statslow);
        }
        ViseLog.d("血压的级别" + stats);
        return stats;
    }

    //判断异常值
    public int crisisVal(float val) {

        int TemperatureSatats = 0;//0正常，1，低热，2高热
        String[] strlow = ConstantManger.LOWHEAT.split("-");
        if (Float.parseFloat(strlow[0]) <= val && val <= Float.parseFloat(strlow[1])) {
            TemperatureSatats = 1;
        }

        String[] strHige = ConstantManger.HIGHFERVER.split("-");
        if (Float.parseFloat(strHige[0]) <= val && val <= Float.parseFloat(strHige[1])) {
            TemperatureSatats = 2;
        }
        return TemperatureSatats;
    }


    //体温异常值判断
    public int tiWen(float val) {
        int TemperatureSatats = 0;
        if (36.0 > val) {
            TemperatureSatats = -1;
        } else if (36.0 <= val && val < 37.3) {
            TemperatureSatats = 0;
        } else if (37.3 <= val && val < 38.1) {
            TemperatureSatats = 1;
        } else {
            TemperatureSatats = 2;
        }
        return TemperatureSatats;
    }

    //添加标题
    public String titleDose(String val) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(val);//标题添加单位
        return stringBuffer.toString();
    }


    //加载头布局------折线图
    View viewhead = null;

    public void addhide(List<TemperatureTableItem> temperatureTableItemList) {
        lineChartView lc_post_day_list = (lineChartView) viewhead.findViewById(R.id.lc_post_day_list);
        lc_post_day_list.setVisibility(View.VISIBLE);
        if (itemName.equals(ConstantManger.TEMPERATURENAME)) {
            lc_post_day_list.referenceValue = ConstantManger.TEMPERATURREFERENCE;
            lc_post_day_list.ic_accuracy = "1";//标识是否折线图上存在小数
        } else if (itemName.equals(ConstantManger.BLOODPRESSURENAME)) {
            lc_post_day_list.referenceValue = ConstantManger.BLOODPRESSUREFERENCEHIGH;
        } else if (itemName.equals(ConstantManger.HEARTRATENAME)) {
            lc_post_day_list.referenceValue = ConstantManger.HEARTRATENAMEFERENCE;
        } else if (itemName.equals(ConstantManger.BILLDSUGAR)) {
            lc_post_day_list.referenceValue = ConstantManger.BILLDSUGARFERENCE;
            lc_post_day_list.ic_accuracy = "1";
        } else if (itemName.equals(ConstantManger.BMI)) {
            lc_post_day_list.referenceValue = ConstantManger.TEMPERATURREFERENCE;//給予参考值，折线图才会绘制
            lc_post_day_list.ic_accuracy = "1";
        } else if (itemName.equals(ConstantManger.flowrate)) {
            lc_post_day_list.referenceValue = "50-100";//給予参考值，折线图才会绘制
        } else {
            lc_post_day_list.ic_accuracy = "1";
            lc_post_day_list.referenceValue = "50-100";//給予参考值，折线图才会绘制
        }
        lc_post_day_list.setLineChar(temperatureTableItemList);
        lc_post_day_list.OnOnTouchListener(new LineChartView.OnTouchListener() {
            float ratio = 1.8f; //水平和竖直方向滑动的灵敏度,偏大是水平方向灵敏
            float x0 = 0f;
            float y0 = 0f;
            float x_src = 0f;
            float y_src = 0f;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        x0 = event.getX();
                        y0 = event.getY();
                        x_src = event.getX();
                        y_src = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        float dx = Math.abs(event.getX() - x_src);
                        float dy = Math.abs(event.getY() - y_src);
                        if (event.getX() - x_src > 50) {
//                            向左滑动
                            a.clear();
                            y.clear();
                            y1.clear();
                            y2.clear();
//                            everydayBrokenLines.clear();
                            isstauts = 1;
                            dailyRecordPresenter.GetUserOneDayDailyRecordData(itemName, timedaylity, "7");
                        } else if (x_src - event.getX() > 50) {
                            //  向右滑动
                            a.clear();
                            y.clear();
                            y1.clear();
                            y2.clear();
//                            everydayBrokenLines.clear();
                            isstauts = 2;
                            dailyRecordPresenter.GetUserOneDayDailyRecordData(itemName, timedaylity, "14");
                        }

                        break;
                    case MotionEvent.ACTION_MOVE:
                        float dxx = Math.abs(event.getX() - x0);
                        float dyx = Math.abs(event.getY() - y0);
                        x0 = event.getX();
                        y0 = event.getY();
                        rv_list.requestDisallowInterceptTouchEvent(dxx * ratio > dyx);
                        break;
                }
                return false;
            }
        });
    }

    private View errorView;

    //初始化柱状图
    List<String> a = new ArrayList<>();  //X轴的标注
    List<String> y = new ArrayList<>();//图表的数据
    List<String> y1 = new ArrayList<>();//图表的数据
    List<String> y2 = new ArrayList<>();//图表的数据

    View viewheadout = null;

    public void initcolumn(final List<String> a, final List<String> y, final List<String> y1, final List<String> y2) {

        ColumnChartView columnchart = (ColumnChartView) viewheadout.findViewById(R.id.column_chart);
        columnchart.setVisibility(View.VISIBLE);
        ViseLog.d(a.size());
        //一个柱状图需要一个柱子集合
        List<Column> columnList = new ArrayList<>();
        //每根柱子又可以分为多根柱子
        List<SubcolumnValue> subcolumnValueList;
        int columns = a.size();//一共7根柱子   //时间长度
        int subColumn = 1;//每根柱子的子柱子为1根
        for (int i = 0; i < columns; i++) {
            subcolumnValueList = new ArrayList<>();
            for (int j = 0; j < subColumn; j++) {
                subcolumnValueList.add(new SubcolumnValue(Float.parseFloat(y.get(i)), Color.parseColor("#01B786")));
                subcolumnValueList.add(new SubcolumnValue(Float.parseFloat(y2.get(i)), Color.parseColor("#4A8A00")));
                subcolumnValueList.add(new SubcolumnValue(Float.parseFloat(y1.get(i)), Color.parseColor("#FFC125")));
            }
            //每根柱子需要一个子柱子集合
            Column column = new Column(subcolumnValueList);
            column.setHasLabels(true);//是否直接显示标注（其它的一些设置类似折线图）
            columnList.add(column);
        }
        ColumnChartData data = new ColumnChartData(columnList);
        Axis axisX = new Axis();
        axisX.setTextColor(Color.parseColor("#000000"));// 灰色
        axisX.setTextSize(11);// 设置字体大小
        axisX.setMaxLabelChars(7); // 最多几个X轴坐标，意思就是你的缩放让X轴上数据的个数7<=x<=mAxisValues.length
        Axis axisY = new Axis().setHasLines(true);
        axisY.hasLines();//是否显示网格线
        axisY.setTextColor(Color.BLACK);//颜色
        //给轴设置值
        axisY.setTextSize(11);// 设置字体大小
        axisY.setTextColor(Color.parseColor("#000000"));
        data.setAxisXBottom(axisX);  //显示X轴
        data.setAxisYLeft(axisY);  //显示Y轴
        List<AxisValue> list = new ArrayList<>();
        for (int i = 0; i < a.size(); i++) {
            list.add(new AxisValue(i).setLabel(a.get(i).substring(5, 10)));//i代表位置，label则是在轴上该位置的标注
        }
        //给x轴设置值
        axisX.setValues(list);
        data.setAxisXBottom(axisX);
        //设置是否让多根子柱子在同一根柱子上显示（会以断层的形式分开），由于这里子柱子只有一根，故设置true也无意义，读者可自行尝试
        data.setStacked(false);
        data.setValueLabelBackgroundColor(Color.parseColor("#161718"));
        data.setValueLabelBackgroundEnabled(false);
        data.setValueLabelsTextColor(Color.parseColor("#161718"));
        columnchart.setZoomType(ZoomType.HORIZONTAL);
        columnchart.setMaxZoom((float) 3);// 缩放比例
        columnchart.setZoomEnabled(true);
        columnchart.setColumnChartData(data);

        columnchart.setOnTouchListener(touchListener());
    }


    public View.OnTouchListener touchListener() {
        View.OnTouchListener touchListener = new View.OnTouchListener() {
            float ratio = 1.8f; //水平和竖直方向滑动的灵敏度,偏大是水平方向灵敏
            float x0 = 0f;
            float y0 = 0f;


            float x_src = 0f;
            float y_src = 0f;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        x0 = event.getX();
                        y0 = event.getY();
                        x_src = event.getX();
                        y_src = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        float dx = Math.abs(event.getX() - x_src);
                        float dy = Math.abs(event.getY() - y_src);
                        if (event.getX() - x_src > 50) {
//                            向左滑动
                            a.clear();
                            y.clear();
                            y1.clear();
                            y2.clear();
//                            everydayBrokenLines.clear();
                            isstauts = 1;
                            dailyRecordPresenter.GetUserOneDayDailyRecordData(itemName, timedaylity, "7");
                        } else if (x_src - event.getX() > 50) {
                            //  向右滑动
                            a.clear();
                            y.clear();
                            y1.clear();
                            y2.clear();
//                            everydayBrokenLines.clear();
                            isstauts = 2;
                            dailyRecordPresenter.GetUserOneDayDailyRecordData(itemName, timedaylity, "14");
                        }

                        break;
                    case MotionEvent.ACTION_MOVE:
                        float dxx = Math.abs(event.getX() - x0);
                        float dyx = Math.abs(event.getY() - y0);
                        x0 = event.getX();
                        y0 = event.getY();
                        rv_list.requestDisallowInterceptTouchEvent(dxx * ratio > dyx);
                        break;
                }
                return false;
            }
        };

        return touchListener;
    }

    String timedaylity = "";

    int isstauts = 1;

    @Override
    public void onDailyRecordDetailsSuccess(AnotherResult<DailyRecordDetailsData> data) {
        List<TemperatureTableItem> temperatureTableItemList = new ArrayList<>();
        if (data.getList() != null && data.getList().size() > 0) {
            if (isstauts == 1) {
                timedaylity = data.getList().get(data.getList().size() - 1).getDailyRecordIndex().getDailyLogBeginTime().substring(0, 10);
                ViseLog.d("位1的时间" + data.getList().get(data.getList().size() - 1).getDailyRecordIndex().getDailyLogBeginTime().substring(0, 10));
            } else if (isstauts == 2) {
                timedaylity = data.getList().get(0).getDailyRecordIndex().getDailyLogBeginTime().substring(0, 10);
                ViseLog.d("位2的时间" + data.getList().get(data.getList().size() - 1).getDailyRecordIndex().getDailyLogBeginTime().substring(0, 10));
            }
        }

        for (int i = 0; i < data.getList().size(); i++) {

            TemperatureTableItem temperatureTableItem = new TemperatureTableItem();
            temperatureTableItem.setDailyItemId(data.getList().get(i).getDailyItemId());
            temperatureTableItem.setLogTime(data.getList().get(i).getLogTime());
            temperatureTableItem.setLogValue(data.getList().get(i).getLogValue());
            temperatureTableItem.setDailyitemDose(data.getList().get(i).getDailyitemDose());
            temperatureTableItem.setDailyitemName(data.getList().get(i).getDailyitemName());

            temperatureTableItem.setItemType(TemperatureTableItem.BODY);
            if (iteminfo != null) {
                temperatureTableItem.setReferenceValue(iteminfo.getStandard());//参考值
                if (data.getList().get(i).getDailyitemName().equals(ConstantManger.BLOODPRESSURENAME)) {
                    temperatureTableItem.setPaired(data.getList().get(i).getPaired());
                }
            } else {
                if (data.getList().get(i).getDailyitemName().equals(ConstantManger.BLOODPRESSURENAME)) {
                    BigDecimal bds = new BigDecimal(data.getList().get(i).getPaired());
                    temperatureTableItem.setPaired(bds.toPlainString());
                }
            }
            temperatureTableItemList.add(temperatureTableItem);
        }


        if (temperatureTableItemList.size() != 0) {
            if (!itemName.equals(ConstantManger.INOUTPUTVAL)) {
                Collections.sort(temperatureTableItemList, new Comparator<TemperatureTableItem>() {
                    @Override
                    public int compare(TemperatureTableItem o1, TemperatureTableItem o2) {
                        if (o1.getLogTime() != null && o2.getLogTime() != null) {
                            Date date1 = DateUtil.stringToDatess(o1.getLogTime());
                            Date date2 = DateUtil.stringToDatess(o2.getLogTime());
                            if (date1.after(date2)) {
                                return 1;
                            }
                        }
                        return -1;
                    }
                });

                for (int i = 0; i < temperatureTableItemList.size(); i++) {
                    temperatureTableItemList.get(i).setLogTime(temperatureTableItemList.get(i).getLogTime().substring(5, 10));
                }

                addhide(temperatureTableItemList);
            }
        }

        List<TemperatureTableItem> TableItemList = new ArrayList<>();
        String doseNameTEMPERATURENAME="";
        if (itemName.equals(ConstantManger.TEMPERATURENAME)) {//体温
            for (int i = 0; i < data.getList().size(); i++) {
                TemperatureTableItem temperatureTableItem = new TemperatureTableItem();
                temperatureTableItem.setDailyItemId(data.getList().get(i).getDailyItemId());
                temperatureTableItem.setLogTime(data.getList().get(i).getLogTime());
                temperatureTableItem.setLogValue(data.getList().get(i).getLogValue());
                temperatureTableItem.setDailyitemDose(data.getList().get(i).getDailyitemDose());
                temperatureTableItem.setDailyitemName(data.getList().get(i).getDailyitemName());
                temperatureTableItem.setItemType(TemperatureTableItem.ONE);
                temperatureTableItem.setReferenceValue(ConstantManger.TEMPERATURREFERENCE);//参考值
                temperatureTableItem.setStats(tiWen(Float.parseFloat(data.getList().get(i).getLogValue())) + "");
                TableItemList.add(temperatureTableItem);
                if(i==0){
                    doseNameTEMPERATURENAME=data.getList().get(i).getDailyitemDose();
                }
            }

            Collections.sort(TableItemList, new Comparator<TemperatureTableItem>() {
                @Override
                public int compare(TemperatureTableItem o1, TemperatureTableItem o2) {
                    Date date1 = DateUtil.stringToDate(o1.getLogTime());
                    Date date2 = DateUtil.stringToDate(o2.getLogTime());
                    if (date1.before(date2)) {
                        return 1;
                    }
                    return -1;
                }
            });
            TemperatureTableItem tableItem = new TemperatureTableItem();
            tableItem.setItemType(TemperatureTableItem.ONE);
            tableItem.setDailyitemName("状态");
            tableItem.setDailyitemDose(doseNameTEMPERATURENAME);
            tableItem.setLogValue(titleDose(ConstantManger.COMPANYNAME));
            TableItemList.add(0, tableItem);
        } else if (itemName.equals(ConstantManger.HEARTRATENAME)) {//心率
            String doseNameHEARTRATENAMEFERENCE="";
            for (int i = 0; i < data.getList().size(); i++) {
                TemperatureTableItem temperatureTableItem = new TemperatureTableItem();
                temperatureTableItem.setDailyItemId(data.getList().get(i).getDailyItemId());
                temperatureTableItem.setLogTime(data.getList().get(i).getLogTime());
                temperatureTableItem.setLogValue(data.getList().get(i).getLogValue());//检测值
                temperatureTableItem.setDailyitemDose(data.getList().get(i).getDailyitemDose());
                temperatureTableItem.setDailyitemName(data.getList().get(i).getDailyitemName());
                temperatureTableItem.setItemType(TemperatureTableItem.ONE);
                temperatureTableItem.setReferenceValue(ConstantManger.HEARTRATENAMEFERENCE);//参考值
                temperatureTableItem.setStats(OutliersValues.hearRate(Float.parseFloat(data.getList().get(i).getLogValue())) + "");
                TableItemList.add(temperatureTableItem);
                if(i==0){
                    doseNameHEARTRATENAMEFERENCE=data.getList().get(i).getDailyitemDose();
                }
            }

            Collections.sort(TableItemList, new Comparator<TemperatureTableItem>() {
                @Override
                public int compare(TemperatureTableItem o1, TemperatureTableItem o2) {
                    Date date1 = DateUtil.stringToDate(o1.getLogTime());
                    Date date2 = DateUtil.stringToDate(o2.getLogTime());
                    if (date1.before(date2)) {
                        return 1;
                    }
                    return -1;
                }
            });

            TemperatureTableItem tableItem = new TemperatureTableItem();
            tableItem.setItemType(TemperatureTableItem.ONE);
            tableItem.setDailyitemName("状态");
            tableItem.setDailyitemDose(doseNameHEARTRATENAMEFERENCE);
            TableItemList.add(0, tableItem);
        } else if (itemName.equals(ConstantManger.BLOODPRESSURENAME)) {//血压


            for (int i = 0; i < data.getList().size(); i++) {
                TemperatureTableItem temperatureTableItem = new TemperatureTableItem();
                temperatureTableItem.setDailyItemId(data.getList().get(i).getDailyItemId());
                temperatureTableItem.setLogTime(data.getList().get(i).getLogTime());
                temperatureTableItem.setLogValue(data.getList().get(i).getLogValue());
                temperatureTableItem.setPaired(data.getList().get(i).getPaired());
                temperatureTableItem.setItemType(data.getList().get(i).getLogType());
                temperatureTableItem.setDailyitemDose(data.getList().get(i).getDailyitemDose());
                temperatureTableItem.setDailyitemName(data.getList().get(i).getDailyitemName());
                temperatureTableItem.setItemType(TemperatureTableItem.THREE);
                temperatureTableItem.setPaired(data.getList().get(i).getPaired());
                int val = Valstats(data.getList().get(i).getLogValue(), data.getList().get(i).getPaired());
                ViseLog.d("第二条先得值" + data.getList().get(i).getPaired());
                temperatureTableItem.setStats(val + "");

                temperatureTableItem.setReferenceValue(ConstantManger.BLOODPRESSUREFERENCEHIGH);//参考值
                TableItemList.add(temperatureTableItem);
            }

            Collections.sort(TableItemList, new Comparator<TemperatureTableItem>() {
                @Override
                public int compare(TemperatureTableItem o1, TemperatureTableItem o2) {
                    Date date1 = DateUtil.stringToDate(o1.getLogTime());
                    Date date2 = DateUtil.stringToDate(o2.getLogTime());
                    if (date1.before(date2)) {
                        return 1;
                    }
                    return -1;
                }
            });


            TemperatureTableItem item = new TemperatureTableItem();
            item.setLogValue(titleDose(ConstantManger.BLOODPRESSURECOMPANYNAME));
            item.setItemType(TemperatureTableItem.TWO);

            TableItemList.add(0, item);
        } else if (itemName.equals(ConstantManger.BILLDSUGAR)) {//血糖

            for (int i = 0; i < data.getList().size(); i++) {
                TemperatureTableItem temperatureTableItem = new TemperatureTableItem();
                temperatureTableItem.setDailyItemId(data.getList().get(i).getDailyItemId());
                temperatureTableItem.setLogTime(data.getList().get(i).getLogTime());
                temperatureTableItem.setLogValue(data.getList().get(i).getLogValue());
                temperatureTableItem.setDailyitemDose(data.getList().get(i).getDailyitemDose());
                if (data.getList().get(i).getMarkValue() != null && data.getList().get(i).getMarkValue().length() > 0) {
                    temperatureTableItem.setDailyitemName(data.getList().get(i).getMarkValue());//data.getList().get(i).getDailyitemName()
                } else {
                    temperatureTableItem.setDailyitemName(data.getList().get(i).getDailyitemName());
                }
                temperatureTableItem.setItemType(TemperatureTableItem.FOUR);
                temperatureTableItem.setReferenceValue(ConstantManger.BILLDSUGARFERENCE);//参考值
                temperatureTableItem.setStats(OutliersValues.bloodSugar(Float.parseFloat(data.getList().get(i).getLogValue())) + "");


                TableItemList.add(temperatureTableItem);
            }
            Collections.sort(TableItemList, new Comparator<TemperatureTableItem>() {
                @Override
                public int compare(TemperatureTableItem o1, TemperatureTableItem o2) {
                    Date date1 = DateUtil.stringToDate(o1.getLogTime());
                    Date date2 = DateUtil.stringToDate(o2.getLogTime());
                    if (date1.before(date2)) {
                        return 1;
                    }
                    return -1;
                }
            });

            TemperatureTableItem tableItem = new TemperatureTableItem();
            tableItem.setDailyitemName("检测项");
            tableItem.setItemType(TemperatureTableItem.FOUR);
            tableItem.setLogValue(titleDose(ConstantManger.BILLDSUGARDOSE));
            TableItemList.add(0, tableItem);
        } else if (itemName.equals(ConstantManger.INOUTPUTVAL)) {//INOUTPUTVAL
            try {
                Map<String, String> map = new HashMap<>();

                int outSum = 0;
                int inSum = 0;
                for (int i = 0; i < data.getList().size(); i++) {
                    TemperatureTableItem temperatureTableItem = new TemperatureTableItem();
                    temperatureTableItem.setId(data.getList().get(i).getId() + "");
                    temperatureTableItem.setLogTime(data.getList().get(i).getLogTime());
                    BigDecimal bdsss = new BigDecimal(data.getList().get(i).getLogValue());
                    temperatureTableItem.setLogValue(bdsss.toPlainString());
                    BigDecimal bdparss = new BigDecimal(data.getList().get(i).getPaired());
                    temperatureTableItem.setPaired(bdparss.toPlainString());
                    temperatureTableItem.setItemType(data.getList().get(i).getLogType());
                    temperatureTableItem.setDailyitemDose(data.getList().get(i).getDailyitemDose());
                    if (data.getList().get(i).getMarkValue() != null) {
                        temperatureTableItem.setMarkValues(data.getList().get(i).getMarkValue());
                    }
                    BigDecimal bd = new BigDecimal(data.getList().get(i).getLogValue());
                    if (Integer.parseInt(bd.toPlainString()) > 0) {
                        temperatureTableItem.setDailyitemName("出量");
                    } else {
                        temperatureTableItem.setDailyitemName("入量");
                    }
                    temperatureTableItem.setItemType(TemperatureTableItem.SIX);
                    temperatureTableItem.setStats(0 + "");//2代表正常
                    temperatureTableItem.setReferenceValue(ConstantManger.BLOODPRESSUREFERENCEHIGH);//参考值
                    TableItemList.add(temperatureTableItem);

                    BigDecimal bds = new BigDecimal(data.getList().get(i).getLogValue());
                    outSum = outSum + Integer.parseInt(bds.toPlainString());
                    BigDecimal bdpar = new BigDecimal(data.getList().get(i).getPaired());
                    inSum = inSum + Integer.parseInt(bdpar.toPlainString());
                    map.put(data.getList().get(i).getLogTime().substring(0, 10), data.getList().get(i).getLogValue());
                }


                Collections.sort(TableItemList, new Comparator<TemperatureTableItem>() {
                    @Override
                    public int compare(TemperatureTableItem o1, TemperatureTableItem o2) {
                        Date date1 = DateUtil.stringToDate(o1.getLogTime());
                        Date date2 = DateUtil.stringToDate(o2.getLogTime());
                        if (date1.before(date2)) {
                            return 1;
                        }
                        return -1;
                    }
                });

                Map<String, List<OutJionValueDataBean>> map1 = new HashMap<>();
                for (String times : map.keySet()) {
                    int sumLogValue = 0, JoinValue = 0;
                    int sumNight = 0;
                    List<OutJionValueDataBean> strings = new ArrayList<>();
                    for (int z = 0; z < data.getList().size(); z++) {
                        if (times.equals(data.getList().get(z).getDailyRecordIndex().getDailyLogBeginTime().substring(0, 10))) {
                            BigDecimal bdValue = new BigDecimal(data.getList().get(z).getLogValue());
                            sumLogValue += Integer.parseInt(bdValue.toPlainString());
                            BigDecimal bdpar = new BigDecimal(data.getList().get(z).getPaired());
                            JoinValue += Integer.parseInt(bdpar.toPlainString());
                            if (data.getList().get(z).getMarkValue().equals(ConstantManger.night)) {
                                BigDecimal nightval = new BigDecimal(data.getList().get(z).getLogValue());
                                sumNight = sumNight + Integer.parseInt(nightval.toPlainString());
                            } else {
                                sumNight = sumNight + 0;
                            }
                        }
                    }
                    strings.add(new OutJionValueDataBean(sumLogValue + "", JoinValue + "", sumNight + ""));
                    map1.put(times, strings);
                }

                List<OutJionValueDataBean> outJionValueDataBeen = new ArrayList<>();

                for (Map.Entry<String, List<OutJionValueDataBean>> entry : map1.entrySet()) {
                    OutJionValueDataBean outJionValueDataBean = new OutJionValueDataBean("", "", "");
                    outJionValueDataBean.setTimeValue(entry.getKey());
                    for (OutJionValueDataBean aa : entry.getValue()) {

                        outJionValueDataBean.setOutValue(aa.getOutValue());
                        outJionValueDataBean.setJionValue(aa.getJionValue());
                        outJionValueDataBean.setNightValue(aa.getNightValue());

                    }
                    outJionValueDataBeen.add(outJionValueDataBean);

                }

                Collections.sort(outJionValueDataBeen, new Comparator<OutJionValueDataBean>() {
                    @Override
                    public int compare(OutJionValueDataBean o1, OutJionValueDataBean o2) {
                        Date date1 = DateUtil.stringToDate(o1.getTimeValue() + " 00:00:00");
                        Date date2 = DateUtil.stringToDate(o2.getTimeValue() + " 00:00:00");
                        if ((date1).after(date2)) {
                            return 1;
                        }
                        return -1;
                    }
                });

                for (int i = 0; i < outJionValueDataBeen.size(); i++) {
                    a.add(outJionValueDataBeen.get(i).getTimeValue());
                    y.add(outJionValueDataBeen.get(i).getOutValue());
                    y1.add(outJionValueDataBeen.get(i).getJionValue());
                    y2.add(outJionValueDataBeen.get(i).getNightValue());
                }

                initcolumn(a, y1, y, y2);

                if (data.getList().size() != 0) {
                    TemperatureTableItem item = new TemperatureTableItem();
                    item.setLogValue(titleDose(ConstantManger.INOUTPUTVALUNIT));
                    item.setItemType(TemperatureTableItem.FIVE);
                    TableItemList.add(0, item);
                }
            } catch (Exception e) {
                msg("数据异常");
                TableItemList.clear();
            }
        } else {

            String doseName="";
            String dailyName="";
            for (int i = 0; i < data.getList().size(); i++) {
                TemperatureTableItem temperatureTableItem = new TemperatureTableItem();
                temperatureTableItem.setDailyItemId(data.getList().get(i).getDailyItemId());
                temperatureTableItem.setLogTime(data.getList().get(i).getLogTime());
                temperatureTableItem.setLogValue(data.getList().get(i).getLogValue());
                temperatureTableItem.setDailyitemDose(data.getList().get(i).getDailyitemDose());
                temperatureTableItem.setDailyitemName(data.getList().get(i).getDailyitemName());
                temperatureTableItem.setItemType(TemperatureTableItem.ONE);
                temperatureTableItem.setReferenceValue(ConstantManger.TEMPERATURREFERENCE);//参考值
                temperatureTableItem.setStats(crisisVal(Float.parseFloat(data.getList().get(i).getLogValue())) + "");
                TableItemList.add(temperatureTableItem);
                //将单位保存出去
                if(i==0){
                    doseName=data.getList().get(i).getDailyitemDose();
                    dailyName=data.getList().get(i).getDailyitemName();
                }
            }
            if (data.getList().size() == 1 && data.getList().get(0).getLogValue().equals("0")) {
                TableItemList.clear();
            }

            Collections.sort(TableItemList, new Comparator<TemperatureTableItem>() {
                @Override
                public int compare(TemperatureTableItem o1, TemperatureTableItem o2) {
                    Date date1 = DateUtil.stringToDate(o1.getLogTime());
                    Date date2 = DateUtil.stringToDate(o2.getLogTime());
                    if (date1.before(date2)) {
                        return 1;
                    }
                    return -1;
                }
            });

            TemperatureTableItem tableItem = new TemperatureTableItem();
            tableItem.setItemType(TemperatureTableItem.ONE);
            tableItem.setDailyitemName("状态");
            if(dailyName.equals(ConstantManger.BMI)){
                tableItem.setDailyitemDose("值");
            }else{
                tableItem.setDailyitemDose(doseName);
            }

            TableItemList.add(0, tableItem);
        }
        //判断page是否是下拉还是上拉
        dailyLinAdapter.setNewData(TableItemList);
        //判断是否数据为空
        if (data.getList() == null || data.getList().size() == 0) {
            dailyLinAdapter.removeAllHeaderView();
            try {
                dailyLinAdapter.remove(0);
            }catch (Exception e){
                ViseLog.d("异常"+e);
            }

        }
        dessDialog();

        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rv_list.getParent(), false);
        dailyLinAdapter.setEmptyView(errorView);
    }

    @Override
    public void onError(String msg) {
        dessDialog();
    }

    @Override
    public void onRefreshUI() {
        dailyLinAdapter.notifyDataSetChanged();
    }

    @Override
    public void onSubmitSuccess(DataStateResult dataStateResult) {

    }

    @Override
    public void onUpdateValueSuccess(DataStateResult dataStateResult) {

    }

    @Override
    public void onDeleteValueSuccess(DataStateResult dataStateResult) {

    }
}
